org 07c00h
	jmp start
	SPH db "GXOS"
	systemp dd 0
	extp dd 0
	mainp dw 07dbeh
	part dw 081beh
	buffer dw 08000h
	inodetable dw 0d00h
start:	mov ax,cs
	mov ds,ax
	mov es,ax
	mov ss,ax
main:	mov ax,[mainp]
	cmp ax,07dfeh
	jz nofind
	mov bx,ax
	add ax,010h
	mov [mainp],ax
	mov eax,[bx+12]
	cmp eax,0
	jz ext
	mov eax,[bx+8]
	mov [systemp],eax
	mov bx,[buffer]
	call hd_LBA
	mov eax,[SPH]
	mov edx,[bx+2]
	cmp eax,edx
	jnz main

	mov eax,1
	mov bx,0500h
	call hd_read
	mov eax,2
	mov bx,0900h
	call hd_read
	mov cx,010h
	mov eax,[0908h]
	mov bx,0d00h
inodet:	call hd_read
	inc eax
	add bx,0400h
	loop inodet

	mov bx,04d00h
	mov cx,1
	call inoderead

loader db "load"
	mov edx,[loader]
	mov bx,04d00h
filefind:
	cmp bx,05000h
	jz nofind
	mov al,[bx+6]
	cmp al,4
	jnz nextfile
	mov eax,[bx+8]
	cmp eax,edx
	jnz nextfile
	mov eax,[bx]
	mov cx,ax
	mov bx,07e00h
	call inoderead
	
	jmp 07e00h
ext:	jmp $
nofind:	jmp $
inoderead:
	push bx
	mov bx,[inodetable]
	xor ax,ax
icount:	add ax,080h
	loop icount
	add bx,ax
	mov eax,[bx+028h]
	pop bx
	call hd_read
	ret
nextfile:
	mov ax,[bx+4]
	add bx,ax
	jmp filefind
hd_read:
	push eax
	mov edx,[systemp]
	shl eax,1
	add eax,edx
	call hd_LBA
	pop eax
	ret
hd_LBA:
	push cx
	push bx
	push bx
	push eax
        mov dx,01f7h
ready:
	
        in al,dx
        and al,080h
        cmp al,080h
        jz ready

        mov al,2
        mov dx,01f2h
        out dx,al
        pop eax

	add eax,0e0000000h
        inc dx
        out dx,al
        shr eax,8
	inc dx
	out dx,al
	shr eax,8
	inc dx
	out dx,al
	shr ax,8
	inc dx
	out dx,al
	
        mov al,020h
        inc dx
        out dx,al
check:
        in al,dx
        and al,088h
        cmp al,08h
        jnz check

        pop bx
        mov cx,512
        mov dx,01f0h
read:
        in ax,dx
        mov [ds:bx],ax
        add bx,2
        loop read
	pop bx
	pop cx
        ret

times 510-($-$$) db 0
dw 0xaa55
